package com.powernode.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.powernode.domain.Prod;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface ProdMapper extends BaseMapper<Prod> {
    // 根据商品名称模糊查询商品id
    @Select("SELECT prod_id FROM prod WHERE prod_name LIKE CONCAT('%', #{prodName}, '%')")
    List<Long> getIdsFromProdName(String prodName);

    @Update("update prod " +
            "set total_stocks = total_stocks + #{count}, sold_num = sold_num - #{count}, version = version + 1 " +
            "where prod_id = #{prodId} " +
            "and version = #{version} " +
            "and total_stocks + #{count} >= 0;")
    int changeProdStock(@Param("prodId") Long prodId, @Param("count") Integer count, @Param("version") Integer version);
}